<template>
  <div id="panel">
    <template>
      <el-card class="queryForm">
        <el-form
          ref="queryForm"
          :model="queryParams"
          :inline="true"
          :label-position="'right'"
        >
          <el-form-item label="点位名称：" prop="segmentname">
            <el-input
              v-model="queryParams.segmentname"
              placeholder="请输入内容"
            ></el-input>
          </el-form-item>

          <div class="searchBtnBox">
            <el-button
              type="primary"
              icon="el-icon-search"
              @click="handleQuery"
              style="padding: 10px"
              >查询</el-button
            >
            <el-button @click="resetQuery" style="padding: 10px">
              <i class="iconfont icon-saoba"></i>
              清除
            </el-button>
          </div>
        </el-form>
      </el-card>
      <el-card class="pageContent">
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button
              type="primary"
              plain
              icon="el-icon-plus"
              size="mini"
              @click="handleAdd"
              v-hasPermi="['system:user:add']"
              >新增</el-button
            >
          </el-col>
          <!-- <el-col :span="1.5">
            <el-button
              type="success"
              plain
              icon="el-icon-edit"
              size="mini"
              @click="handleUpdate"
              v-hasPermi="['system:user:edit']"
              >修改</el-button
            >
          </el-col> -->
          <el-col :span="1.5">
            <el-button
              type="danger"
              plain
              icon="el-icon-delete"
              size="mini"
              @click="handleDelete()"
              v-hasPermi="['system:user:remove']"
              >删除</el-button
            >
          </el-col>

          <!-- <el-col :span="1.5">
            <el-button
              type="warning"
              plain
              icon="el-icon-download"
              size="mini"
              @click="handleExport"
              v-hasPermi="['system:user:export']"
              >导出</el-button
            >
          </el-col> -->
        </el-row>
        <el-table
          :data="tableData"
          style="width: 100%"
          v-loading="loading"
          @selection-change="handleSelectionChange"
        >
          <el-table-column
            type="selection"
            width="50"
            align="center"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            prop="segmentname"
            label="点位名称"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            prop="segmtype"
            label="点位类型"
            :show-overflow-tooltip="true"
          >
            <template slot-scope="scope">
              <template v-for="item in dict.type.sys_segmenttype">
                <div :key="item.id" v-if="item.value == scope.row.segmtype">
                  {{ item.label }}
                </div>
              </template>
            </template>
          </el-table-column>
          <el-table-column
            prop="propretyname"
            label="点位属性"
            :show-overflow-tooltip="true"
          />
          <el-table-column
            prop="startdate"
            label="开始执行日期"
            :show-overflow-tooltip="true"
          >
            <template slot-scope="scope">
              {{ parseTime(scope.row.startdate, "{yyyy}-{mm}-{dd}") }}
            </template>
          </el-table-column>
          <el-table-column
            prop="enddate"
            label="结束执行日期"
            :show-overflow-tooltip="true"
          >
            <template slot-scope="scope">
              {{ parseTime(scope.row.enddate, "{yyyy}-{mm}-{dd}") }}
            </template>
          </el-table-column>
          <el-table-column fixed="right" label="操作" align="center">
            <template slot-scope="scope">
              <!-- <el-button type="text" @click="view(scope.row)">查看</el-button> -->
              <el-button type="text" @click="handleUpdate(scope.row)"
                >编辑</el-button
              >
              <el-button type="text" @click="handleDelete(scope.row)"
                >删除</el-button
              >
            </template>
          </el-table-column>
        </el-table>
        <el-pagination
          class="pagination-self"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :pager-count="5"
          :current-page="queryParams.pageNum"
          :page-size="queryParams.pageSize"
          layout="slot, total, sizes, prev, pager, next, jumper"
          :total="tableTotal"
        >
          <el-button
            class="refresh-button"
            icon="el-icon-refresh"
            title="刷新"
            @click="getList"
          />
        </el-pagination>
      </el-card>
    </template>
    <!-- 添加或修改用户配置对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-row>
          <el-col :span="12">
            <el-form-item label="点位类型" prop="segmenttype">
              <el-select
                @change="changeSegmtype"
                v-model="form.segmtype"
                placeholder="请选择点位类型"
                style="width: 100%"
              >
                <el-option
                  v-for="item in dict.type.sys_segmenttype"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="点位名称" prop="segmentname">
              <el-select
                v-model="form.segmentid"
                placeholder="请选择点位名称"
                style="width: 100%"
                @change="
                  formChange(
                    $event,
                    'id',
                    {
                      segmentname: 'segmentname',
                      segmentcode: 'segmentcode',
                      controllevel: 'controllevel',
                      controllevelcode: 'controllevelcode',
                    },
                    dmList
                  )
                "
              >
                <el-option
                  v-for="item in dmList"
                  :key="item.value"
                  :label="item.segmentname"
                  :value="item.id"
                >
                </el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="考核属性" prop="assesstype">
              <el-select
                v-model="form.assesstype"
                placeholder="请选择"
                style="width: 100%"
                @change="
                  formChange(
                    $event,
                    'label',
                    { assesstypecode: 'value' },
                    dict.type.sys_assse_type
                  )
                "
              >
                <el-option
                  v-for="item in dict.type.sys_assse_type"
                  :key="item.value"
                  :label="item.label"
                  :value="item.label"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="点位属性" prop="propretyname">
              <el-select
                v-model="form.propretyname"
                placeholder="请选择点位属性"
                style="width: 100%"
                @change="
                  formChange(
                    $event,
                    'label',
                    { proprety: 'value' },
                    dict.type.segment_prop
                  )
                "
              >
                <el-option
                  v-for="dict in dict.type.segment_prop"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.label"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="执行开始时间" prop="startdate">
              <el-date-picker
                style="width: 100%"
                v-model="form.startdate"
                type="date"
                placeholder="选择日期"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="执行结束时间" prop="enddate">
              <el-date-picker
                style="width: 100%"
                v-model="form.enddate"
                type="date"
                placeholder="选择日期"
              >
              </el-date-picker>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="考核行政区划" prop="assesstowncode">
              <el-select
                v-model="form.assesstowncode"
                @change="
                  formChange($event, 'code', { assesstown: 'name' }, regionList)
                "
                placeholder="请选择行政区划"
                style="width: 100%"
              >
                <el-option
                  v-for="dict in regionList"
                  :key="dict.code"
                  :label="dict.name"
                  :value="dict.code"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="规划年份" prop="planningyear">
              <el-input
                v-model="form.planningyear"
                placeholder="请输入规划年份"
              />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="调整年份" prop="adjustmentyear">
              <el-input
                v-model="form.adjustmentyear"
                placeholder="请输入调整年份"
              />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getDMAttributeList,
  DMAttributeAdd,
  DMAttributeEdit,
  DMAttributeRemove,
} from "@/api/dataResources/monitorData/dmAttribute.js";
import { getRegionList } from "@/api/dataResources/monitorData/region.js";
import { getDmList } from "@/api/dataResources/monitorData/section.js";
export default {
  name: "HydrologicStation",
  dicts: ["sys_segmenttype", "sys_assse_type", "segment_prop"],
  components: {},
  computed: {},
  data() {
    return {
      regionList: [],
      dmList: [],
      loading: false,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        stationName: "",
        riverName: "",
        segmentname: "",
      },
      dateRange: [],
      // 选中数组
      ids: [],
      tableData: [],
      tableTotal: 0,
      // 是否显示新增/修改弹出层
      open: false,
      // 弹出层标题
      title: "",
      // 表单参数
      form: {},
      // 表单校验
      rules: {},
    };
  },
  watch: {},
  mounted() {
    this.getRegionListFn();
    this.getDmListFn();
    this.pageInit();
  },
  methods: {
    changeSegmtype() {
      this.getDmListFn();
    },
    //页面初始化请求的数据
    pageInit() {
      this.getRwly();
      this.getList();
    },
    async getRegionListFn() {
      let res = await getRegionList();
      if (res.code == 200) {
        this.regionList = res.rows;
      }
    },
    async getDmListFn() {
      const query = {
        isdrink: "0",
      };
      if (this.form.segmtype == "1") {
        query.ismanual = "1";
      } else if (this.form.segmtype == "2") {
        query.isauto = "1";
      }
      let res = await getDmList(query);
      if (res.code == 200) {
        this.dmList = res.rows;
      }
    },
    //获取任务来源下拉选项数据
    getRwly() {},
    //表格checkbox选择数据
    handleSelectionChange(selection) {
      this.ids = selection.map((item) => item.id);
    },

    formChange(value, key, map, list) {
      let item = list.find((it) => it[key] == value);
      if (item) {
        for (let fkey in map) {
          this.form[fkey] = item[map[fkey]];
          console.log("fkey", fkey, this.form[fkey]);
        }
      }
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.form = {
        segmentname: "",
        segmentid: "                                ",
        segmentcode: "",
        controllevelcode: "",
        controllevel: "",
        proprety: "",
        propretyname: "",
        startdate: "",
        enddate: "",
        assesstypecode: "",
        assesstype: "",
        assesscity: null,
        assesstown: "",
        assesstowncode: "",
        planningyear: "",
        adjustmentyear: "",
      };
      this.reset();
      this.open = true;
      this.title = "添加";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.form = { ...row };
      this.reset();
      this.open = true;
      this.title = "修改";
      this.getDmListFn();
    },
    /** 删除按钮操作 */
    async handleDelete(row) {
      // 删除弹窗确认
      let res = await this.$confirm("是否确认删除?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          return true;
        })
        .catch(() => {
          return false;
        });
      if (!res) return;
      let id;
      if (row) {
        id = row.id;
      } else {
        id = this.ids.join(",");
      }
      this.$modal
        .confirm("是否确认删？")
        .then(function () {
          return DMAttributeRemove(id);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => {});
    },
    // 行间编辑
    edit(row) {
      this.open = true;
      // this.formType = "edit";
      this.title = "修改";
      this.form = { ...row };
      this.form.lifeStatus = row.lifeStatus === "1";
    },
    // 删除行
    // 分页
    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.queryParams.pageSize = val;
      this.getList();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.queryParams.pageNum = val;
      this.getList();
    },
    //搜索
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    //重置、清除表单
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
    },
    // 弹出层取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.resetForm("form");
    },
    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          this.form.startdate = this.parseTime(
            this.form.startdate,
            "{yyyy}-{mm}-{dd}"
          );
          this.form.enddate = this.parseTime(
            this.form.enddate,
            "{yyyy}-{mm}-{dd}"
          );
          if (this.form.id != undefined) {
            DMAttributeEdit(this.form).then((res) => {
              this.open = false;
              this.$modal.msgSuccess("修改成功");
              this.getList();
            });
          } else {
            DMAttributeAdd(this.form).then((res) => {
              this.open = false;
              this.$modal.msgSuccess("新增成功");
              this.getList();
            });
          }
        }
      });
    },
    //导出
    handleExport() {},
    // 获取表格数据
    async getList() {
      this.loading = true;
      let res = await getDMAttributeList(this.queryParams);
      if (res.code == 200) {
        this.tableData = res.rows || [];
        this.tableTotal = res.total || 0;
      }
      this.loading = false;
    },
  },
};
</script>

<style lang="scss" scoped>
p {
  margin: 0;
}

img {
  vertical-align: middle;
}

#panel {
  padding-bottom: 40px;
  min-height: calc(100% - 20px);
  margin: 0;

  // 控制分页的样式----二次开发
  .pagination-self {
    margin: 20px 0;
    display: flex;
    justify-content: flex-end;
    position: relative;

    .el-pagination__total {
      position: absolute;
      left: 5px;
      color: #409eff;
      font-weight: 600;
      font-size: 15px;
    }
  }

  // 控制分页刷新按钮样式
  .refresh-button {
    .el-icon-refresh {
      font-size: 18px;
      line-height: 28px;
    }
  }

  .el-card {
    margin: 10px;
  }

  .queryForm .el-card__body {
    padding: 0 20px !important;
  }

  .queryForm {
    // .el-card__body {
    //   padding-left: 0 !important;
    // }
    .el-form-item {
      margin: 20px 0 20px 20px;
    }

    .el-form-item:first-child {
      margin-left: 0 !important;
    }

    .el-form-item__label {
      padding-right: 5px;
      color: #555;
    }

    .times {
      .el-form-item__content {
        width: 250px;
      }
    }

    .el-form-item__content {
      min-width: 200px;
      // & > div {
      //   width: 100% !important;
      // }
    }

    .searchBtnBox {
      display: inline-block;
      margin: 20px 0 20px 20px;
    }
  }

  .pageContent {
    .tabs-list {
      padding-top: 20px;
    }

    .el-tabs__header {
      background: #fff;
    }

    .el-tabs__item {
      border: 1px solid #eee;
      margin: 0;
      border-left: 0;
      background: #fafbfc;
      color: #000;
    }

    .el-tabs__item:first-child {
      margin-left: 20px;
      border-left: 1px solid #eee;
    }

    .is-active {
      border-bottom: 0 !important;
      background: #fff;
      color: #307eff;
    }
  }

  .anjian label {
    padding: 0 !important;
    width: 100px !important;
  }

  .queryForm .el-checkbox {
    margin-right: 10px !important;
    width: auto !important;
  }

  .iszgfc .el-form-item__content {
    width: 120px;
  }
}
</style>
